package com.wtwd.device.service.impl;


import com.wtwd.device.service.LocaleSourceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * @author zdl
 * @date 2021/11/25 16:46
 */
@Service
public class PushLocaleSourceServiceImpl implements LocaleSourceService {

    @Resource(name = "pushMessageLocaleResource")
    private MessageSource pushMessageLocaleResource;

    @Resource
    private HttpServletRequest request;

    @Override
    public String getMessage(String messageKey) {
        return pushMessageLocaleResource.getMessage(messageKey, null, RequestContextUtils.getLocale(request));
    }

    @Override
    public String getMessage(String messageKey, String lang) {
        if (StringUtils.isBlank(lang)) {
            lang = DEFAULT_LANG;
        }
        return pushMessageLocaleResource.getMessage(messageKey, null, new Locale(lang));
    }
}
